# Table_A08.R

# Part of the replication archive for 
#
#   Bullock, John G. 2020. "Education and Attitudes toward Redistribution in
#   the United States." British Journal of Political Science 50.


# This file produces Table A8 in the appendix to the article. It reports the  
# main 2SLS estimates. (In the article, these estimates are reported 
# graphically and in prose, but not in a table.)


library(Bullock, lib.loc = c(.libPaths(), 'packageLibrary'))  # for qw()
library(ivpack)   # for cluster.robust.se

source("IV_setup.R")
varNames        <- qw('eqwlth goveqinc guarantee.7pt govt.health.7pt helppoor welfare')
dfNames         <- qw('GSS.df GSS.df   ANES.df       ANES.df         GSS.df   GSS.df') 
modelRownames   <- c(
  'Basic model (Eq. 2)\\lips',
  '\\lips with cohort-year fixed effects',  
  '\\lips with state-when-young $\\times$ year-when-young trends',
  '\\lips with political and demographic controls')
modelColnames <- list(
  c('redistrib.',  'redistrib.',  'guarantee', 'health', 'help',  ''),
  c('to poor (1)', 'to poor (2)', 'SOL',       'care',   'poor',  'welfare'),
  c('(GSS)',       '(GSS)',       '(ANES)',    '(ANES)', '(GSS)', '(GSS)'))



##############################################################################
# ESTIMATE MAIN IV RESULTS 
##############################################################################
IVModelObjectsEnv <- estimateModels(
  varNames     = varNames, 
  modelEnvir   = IVModelsEnv, 
  dfNames      = dfNames,
  objectSuffix = '.IV') 
mainEstsEduc <- estTable(
  varNames    = varNames, 
  treatment   = 'educ', 
  objectEnvir = IVModelObjectsEnv,
  clusterSEs  = TRUE)



##############################################################################
# LATEX TABLE OF MAIN ESTIMATES
##############################################################################
nMinEduc <- apply(
  X      = getNobsForMatrixOfModels(varNames, objectEnvir = IVModelObjectsEnv), 
  MARGIN = 2, 
  FUN    = min)
nMaxEduc <- apply(
  X      = getNobsForMatrixOfModels(varNames, objectEnvir = IVModelObjectsEnv), 
  MARGIN = 2, 
  FUN    = max)
footerRows <- list(
  c('Number of', nMinEduc),
  c('observations', paste0('to ', nMaxEduc)))  
modelRownamesShort <- qw("(1) (2) (3) (4)")  
mainEstsLaTeXTable <- latexTable(
  mat            = mainEstsEduc,
  decimalPlaces  = 3,
  rowNames       = modelRownamesShort,
  colNames       = modelColnames,  
  spacerColumns  = seq(0, ncol(mainEstsEduc) - 1, by = 2),  # after rowname and each SE column
  extraRowHeight = '3.25pt',
  headerFooter   = TRUE,
  footerRows     = footerRows,  
  commandName    = 'TabIVMainResults',
  caption        = "Each entry is an estimate or a standard error from a separate two-stage least squares regression.  This table corresponds exactly to \\autoref{FigIVMainResults}; the difference is that it presents the estimates and standard errors in tabular form rather than graphically.",
  captionMargins = qw(".1in .1in"),
  callCommand    = FALSE)

# TWEAK THE TABLE FORMATTING
myIndex <- grep('        observations', mainEstsLaTeXTable)
mainEstsLaTeXTable <- c(
  mainEstsLaTeXTable[1:(myIndex-1)],
  '        \\addlinespace[-.6ex]',
  mainEstsLaTeXTable[myIndex:length(mainEstsLaTeXTable)])
class(mainEstsLaTeXTable) <- c('latexTable', 'character')

# MAKE THE .TEX FILE
latexTablePDF(
  latexCommands      = list(mainEstsLaTeXTable), 
  firstPageEmpty     = FALSE,
  wrapper            = FALSE,                     
  outputFilenameStem = 'float_output/Table_A08',            
  overwriteExisting  = TRUE,
  keepPDFFile        = FALSE,
  keepTexFile        = TRUE,
  openPDFOnExit      = FALSE)
